package model

import (
	"errors"
	"gorm.io/gorm"
	"saleforce/sf-backend-user/constant"
	"time"
)

type Role struct {
	Id       string    `gorm:"primary_key;column:id;type:varchar(255)" json:"id"`
	RoleName string    `gorm:"column:role_name;type:varchar(255)" json:"role_name"`
	Status   int32     `gorm:"column:status;type:tinyint" json:"status"`
	Menus    string    `gorm:"column:menus;type:text" json:"menus"`
	IsSys    int32     `gorm:"column:is_sys;type:tinyint" json:"is_sys"`
	Created  time.Time `gorm:"column:created;type:timestamp" json:"created"`
	Updated  time.Time `gorm:"column:updated;type:timestamp" json:"updated"`
	IsDel    int32     `gorm:"column:is_del;type:tinyint" json:"is_del"`
}

// GetById 通过id查询
func (r Role) GetById(id string) (*Role, error) {
	tx := constant.Db.Model(r).Omit("is_del").Where("id = ?", id).First(&r)
	if tx.Error != nil {
		if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
			return nil, nil
		} else {
			return nil, errors.New("Query error")
		}
	}
	return &r, nil
}

func (Role) TableName() string {
	return "sf-role"
}
